package com.tangshuai.video.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tangshuai.video.mapper.VideoInfoMapper;
import com.tangshuai.video.po.VideoInfo;
import com.tangshuai.video.service.VideoInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @Author TANGSHUAI
 * @Date 2024-03-03 22:14
 **/
@Service
public class VideoInfoServiceImpl implements VideoInfoService {

    @Autowired
    private VideoInfoMapper videoInfoMapper;

    @Override
    public IPage<VideoInfo> selectAllVideoInfo(Integer pageNo, Integer pageSize,VideoInfo videoInfo) {
        IPage page = new Page(pageNo, pageSize);
        LambdaQueryWrapper<VideoInfo> queryWrapper=new LambdaQueryWrapper<>();
        if(null!=videoInfo && !StringUtils.isEmpty(videoInfo.getVideoName())){
            queryWrapper.like(VideoInfo::getVideoName,videoInfo.getVideoName());
        }
        if(null!=videoInfo && !StringUtils.isEmpty(videoInfo.getVideoClassification())){
            queryWrapper.like(VideoInfo::getVideoClassification,videoInfo.getVideoClassification());
        }
        // 按创建时间排序
        queryWrapper.orderByDesc(VideoInfo::getCreate_time);
        return videoInfoMapper.selectPage(page, queryWrapper);
    }

    @Override
    public VideoInfo selectByIdVideoInfo(Integer id) {
        return videoInfoMapper.selectById(id);
    }

    @Override
    public Integer addVideoInfo(VideoInfo videoInfo) {
        return videoInfoMapper.insert(videoInfo);
    }

    @Override
    public Integer updateVideoInfo(VideoInfo videoInfo) {
        return videoInfoMapper.updateById(videoInfo);
    }

    @Override
    public VideoInfo selectByVideoName(String directory) {
        LambdaQueryWrapper<VideoInfo> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(VideoInfo::getVideoName,directory);
        return videoInfoMapper.selectOne(queryWrapper);
    }
}
